package com.lihang;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import ohos.agp.render.*;
import ohos.agp.utils.Matrix;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

import java.security.MessageDigest;


/**
 * Created by leo
 * on 2019/12/2.
 * <p>
 * 这个是glide只加载2个圆角
 */
class GlideRoundTransform implements Transformation<PixelMap> {
    private BitmapPool mBitmapPool;

    private float leftTop_radius;
    private float leftBottom_radius;
    private float rightTop_radius;
    private float rightBottom_radius;
    private boolean isLeftTop, isRightTop, isLeftBottom, isRightBottom;


    /**
     * @param context 上下文
     */
    public GlideRoundTransform(Context context, float leftTop_radius, float leftBottom_radius, float rightTop_radius, float rightBottom_radius) {
        this.mBitmapPool = Glide.get(context).getBitmapPool();
        this.leftTop_radius = leftTop_radius;
        if (leftTop_radius != 0) {
            isLeftTop = true;
        }
        this.leftBottom_radius = leftBottom_radius;
        if (leftBottom_radius != 0) {
            isLeftBottom = true;
        }
        this.rightTop_radius = rightTop_radius;
        if (rightTop_radius != 0) {
            isRightTop = true;
        }
        this.rightBottom_radius = rightBottom_radius;
        if (rightBottom_radius != 0) {
            isRightBottom = true;
        }
    }

    @Override
    public Resource<PixelMap> transform(Context context, Resource<PixelMap> resource, int outWidth, int outHeight) {
        PixelMap source = resource.get();
        int finalWidth, finalHeight;
        //输出目标的宽高或高宽比例
        float scale;
        if (outWidth > outHeight) {
            //如果 输出宽度 > 输出高度 求高宽比

            scale = (float) outHeight / (float) outWidth;
            finalWidth = source.getImageInfo().size.width;
            //固定原图宽度,求最终高度
            finalHeight = (int) ((float) source.getImageInfo().size.width * scale);
            if (finalHeight > source.getImageInfo().size.height) {
                //如果 求出的最终高度 > 原图高度 求宽高比

                scale = (float) outWidth / (float) outHeight;
                finalHeight = source.getImageInfo().size.height;
                //固定原图高度,求最终宽度
                finalWidth = (int) ((float) source.getImageInfo().size.height * scale);
            }
        } else if (outWidth < outHeight) {
            //如果 输出宽度 < 输出高度 求宽高比

            scale = (float) outWidth / (float) outHeight;
            finalHeight = source.getImageInfo().size.height;
            //固定原图高度,求最终宽度
            finalWidth = (int) ((float) source.getImageInfo().size.height * scale);
            if (finalWidth > source.getImageInfo().size.width) {
                //如果 求出的最终宽度 > 原图宽度 求高宽比

                scale = (float) outHeight / (float) outWidth;
                finalWidth = source.getImageInfo().size.width;
                finalHeight = (int) ((float) source.getImageInfo().size.width * scale);
            }
        } else {
            //如果 输出宽度=输出高度
            finalHeight = source.getImageInfo().size.height;
            finalWidth = finalHeight;
        }

        //修正圆角
        this.leftTop_radius *= (float) finalHeight / (float) outHeight;
        this.leftBottom_radius *= (float) finalHeight / (float) outHeight;
        this.rightTop_radius *= (float) finalHeight / (float) outHeight;
        this.rightBottom_radius *= (float) finalHeight / (float) outHeight;
        PixelMap outBitmap = this.mBitmapPool.get(finalWidth, finalHeight, PixelFormat.ARGB_8888);
        if (outBitmap == null) {
            PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
            options.size = new Size(finalWidth, finalHeight);
            options.pixelFormat = PixelFormat.ARGB_8888;
            outBitmap = PixelMap.create(options);
        }

        Texture texture = new Texture(outBitmap);
        Canvas canvas = new Canvas(texture);
        Paint paint = new Paint();
        //关联画笔绘制的原图bitmap
        PixelMapShader shader = new PixelMapShader(new PixelMapHolder(source), Shader.TileMode.CLAMP_TILEMODE, Shader.TileMode.CLAMP_TILEMODE);
        //计算中心位置,进行偏移
        Size size = source.getImageInfo().size;
        int width = (size.width - finalWidth) / 2;
        int height = (size.height - finalHeight) / 2;
        if (width != 0 || height != 0) {
            Matrix matrix = new Matrix();
            matrix.setTranslate((float) (-width), (float) (-height));
            shader.setShaderMatrix(matrix);
        }

        paint.setShader(shader, Paint.ShaderType.PIXELMAP_SHADER);
        paint.setAntiAlias(true);
        RectFloat rectF = new RectFloat(0.0F, 0.0F, (float) texture.getWidth(), (float) texture.getHeight());

        float[] outerR = new float[]{leftTop_radius, leftTop_radius, rightTop_radius, rightTop_radius, rightBottom_radius, rightBottom_radius, leftBottom_radius, leftBottom_radius};//左上，右上，右下，左下
        Path path = new Path();
        path.addRoundRect(rectF, outerR, Path.Direction.CLOCK_WISE);
        canvas.drawPath(path, paint);

        return BitmapResource.obtain(outBitmap, this.mBitmapPool);
    }


    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {
    }
}

